package com.android.lovepick.map;

import java.util.List;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;

import com.android.lovepick.util.PathInfo;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.Overlay;
import com.baidu.mapapi.Projection;

public class PathOverlay extends Overlay
{
	// private final String PATH_OVERLAY_TAG = "PathOverlay";
	private List<PathInfo> mPathInfos = null;
	private Projection mProjection;
	private Paint mPaint = null;

	/**
	 * @param projection
	 * @param pathInfos 路径上的信息点
	 * @param color 路线颜色值
	 */
	public PathOverlay(Projection projection, List<PathInfo> pathInfos,
			int color)
	{
		this.mProjection = projection;
		this.mPathInfos = pathInfos;

		// setting the paint
		mPaint = new Paint();
		mPaint.setColor(color);
		mPaint.setStrokeWidth(10);
		mPaint.setAntiAlias(false);
		mPaint.setStrokeMiter(3);
		mPaint.setStyle(Style.STROKE);
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow)
	{
		super.draw(canvas, mapView, shadow);
		if (shadow)
		{
			return;
		}

		Path path = new Path();
		Point pixelPoint = new Point();

		// 获取屏幕高度和宽度
		int width = canvas.getWidth();
		int height = canvas.getHeight();

		GeoPoint bottomRight = mProjection.fromPixels(width, height);
		GeoPoint topLeft = mProjection.fromPixels(0, 0);

		int maxLat = topLeft.getLatitudeE6();
		int minLat = bottomRight.getLatitudeE6();
		int minLng = topLeft.getLongitudeE6();
		int maxLng = bottomRight.getLongitudeE6();

		// 之前的布点是否在屏幕中
		boolean preOutOfBounds = true;
		// 之前的布点是否是新的一段路线的开始
		boolean preIsMoveTo = true;

		int moveToLat = Integer.MAX_VALUE;
		int moveToLng = Integer.MIN_VALUE;

		boolean first = true;

		for (PathInfo pathInfo : mPathInfos)
		{
			// Log.i(PATH_OVERLAY_TAG, "pathInfo: " + pathInfo.toString());
			int latE6 = (int) (Double.parseDouble(pathInfo.getLatitude()) * 1E6);
			int lngE6 = (int) (Double.parseDouble(pathInfo.getLongitude()) * 1E6);

			// 判断当前点是否超出屏幕显示的范围
			boolean currentOutOfBounds = (latE6 < minLat) || (latE6 > maxLat)
					|| (lngE6 < minLng) || (lngE6 > maxLng);

			// 起点, preIsMoveTo = true;
			// 先前点和当前点都在屏幕之外, preIsMoveTo = true;
			// 当前坐标在屏幕外, 并且先前坐标也在屏幕外, preIsMoveTo = true.
			if (first || (preOutOfBounds && currentOutOfBounds))
			{
				moveToLat = latE6;
				moveToLng = lngE6;
				path.moveTo(pixelPoint.x, pixelPoint.y);
				first = false;
				preIsMoveTo = true;
			}
			else
			{
				if (preIsMoveTo)
				{
					// 绘制新的一段路线的起点
					GeoPoint geoPoint = new GeoPoint(moveToLat, moveToLng);
					// 将经纬度转换成屏幕画布上的点(即布点)
					mProjection.toPixels(geoPoint, pixelPoint);

					// 绘制新路线的起点
					path.moveTo(pixelPoint.x, pixelPoint.y);
					preIsMoveTo = false;
				}
				GeoPoint geoPoint = new GeoPoint(latE6, lngE6);
				mProjection.toPixels(geoPoint, pixelPoint);
				// 最后的布点与当前布点之间画一条线.
				path.lineTo(pixelPoint.x, pixelPoint.y);
			}
			preOutOfBounds = currentOutOfBounds;
		}
		canvas.drawPath(path, mPaint);
	}

}
